package com.uphone.kingmall.base;

import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.SizeUtils;
import com.china.zhuoyue.R;
import com.uphone.kingmall.utils.CommonUtil;
import com.uphone.kingmall.view.indicator.IndicatorAdapter0;
import com.uphone.kingmall.view.indicator.TrackIndicatorView;

import butterknife.BindView;

/**
 * <pre>
 *     author : radish
 *     e-mail : 15703379121@163.com
 *     time   : 2019/3/19
 *     desc   :
 * </pre>
 */
public abstract class BaseTabActivity0 extends BaseActivity {

    @BindView(R.id.track_indicator)
    protected TrackIndicatorView trackIndicator;
    @BindView(R.id.viewPager)
    protected ViewPager viewPager;
    @BindView(R.id.ll)
    protected LinearLayout ll;
    private Fragment[] fragmentList;
    protected boolean isLoad = true;
    protected int msgCountB = 0;
    protected int positionB = 0;

    @Override
    public void initView() {
        CommonUtil.initTitle(this, initTitle(), ll);
    }

    protected abstract String initTitle();

    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_activity_base_tab;
    }

    @Override
    public void initData() {
        if (isLoad) {
            trackIndicator.setBottomTrackWidth(SizeUtils.dp2px(20));
            trackIndicator.setVisibleNum(2);
            fragmentList = getFragmentList();
            initViewPager();
            initIndicator();
            viewPager.setCurrentItem(positionB);
        }
    }

    protected abstract Fragment[] getFragmentList();

    private void initViewPager() {
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragmentList[i];
            }

            @Override
            public int getCount() {
                return fragmentList.length;
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int position) {

            }
        });
    }

    /**
     * 添加指示器
     */
    private void initIndicator() {
        trackIndicator.setAdapter(viewPager, new IndicatorAdapter0(this, getItems(), msgCountB));
    }

    protected abstract String[] getItems();
    //    @BindView(R.id.sliding_tab_layout)
//    SlidingTabLayout slidingTabLayout;
//    @BindView(R.id.viewpager)
//    ViewPager viewpager;
//    @BindView(R.id.ll)
//    LinearLayout ll;
//    protected int index = 0;
//
//    private Fragment[] fragments;
//
//    @Override
//    public int getLayoutContentId() {
//        return R.layout.activity_base_tab0;
//    }
//
//    @Override
//    public void initView() {
//        CommonUtil.initTitle(this, initTitle(), ll);
//
//        fragments = getFragments();
//        MyFragmentPageAdapter fragmentPageAdapter = new MyFragmentPageAdapter(getSupportFragmentManager());
//        viewpager.setAdapter(fragmentPageAdapter);
//        slidingTabLayout.setViewPager(viewpager, getTitles());
//        viewpager.setCurrentItem(index);
//    }
//
//    protected abstract String initTitle();
//
//    protected abstract Fragment[] getFragments();
//
//    protected abstract String[] getTitles();
//
//    @Override
//    public void initData() {
//
//    }
//
//    public class MyFragmentPageAdapter extends FragmentPagerAdapter {
//
//        public MyFragmentPageAdapter(FragmentManager fm) {
//            super(fm);
//        }
//
//        @Override
//        public Fragment getItem(int i) {
//            return fragments[i];
//        }
//
//        @Override
//        public int getCount() {
//            return fragments.length;
//        }
//
//    }
}
